package com.school.graduation.goods.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.goods.presenter.FoundGoodDetailAdapterNew;
import com.school.graduation.goods.presenter.FoundGoodDetailsPresenter;
import com.school.graduation.util.NetWorkUtil;

import butterknife.Bind;
import butterknife.OnClick;

public class FoundGoodDetailsActivityNew extends BaseActivity
        implements ImplHotMarketDetails  {
    @Bind(R.id.hot_top_quit)
    ImageView hotTopQuit;
    @Bind(R.id.hot_top_find)
    ImageView hotTopFind;
    @Bind(R.id.hot_market_detail_recycler)
    RecyclerView hotMarketDetailRecycler;
    @Bind(R.id.hot_top_title)
    TextView hotTopTitle;

    private FoundGoodDetailsPresenter presenter;
    private MaterialDialog progressDialog;
    private boolean hot;

    public static void toDetail(Context context, Bundle bundle) {
        Intent intent = new Intent(context, FoundGoodDetailsActivityNew.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_hot_market_detail_new;
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("item");
        initTitle(title);
        initPresenter();
        hot = intent.getBooleanExtra("hot",false);
        judgeNetWork(title);         //判断网络状态

    }

    /*
    网络连接，获取数据
    网络不连接，弹dialog
     */
    private void judgeNetWork(String title) {
        if (NetWorkUtil.isNetWorkConnected(context)){
            initGoodsRecycler(title);
        }else {
            new MaterialDialog.Builder(context)
                    .title("提示")
                    .content("检测到手机没有网络，请连接网络后重试")
                    .positiveText("确定")
                    .positiveColor(getResources().getColor(R.color.md_blue_200))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            finish();
                        }
                    }).canceledOnTouchOutside(false)
                    .build().show();
        }
    }

    private void initGoodsRecycler(String title) {
        hotMarketDetailRecycler.setLayoutManager(new GridLayoutManager(context,2));
        showProgressDialog();
        if (hot) presenter.getListBySortNew(title);
        else presenter.getListsByInputNew(title);
    }

    private void initPresenter() {
        presenter = new FoundGoodDetailsPresenter(context,this);                              //初始化处理器
    }

    private void initTitle(String theItemToHere) {
        hotTopTitle.setText(theItemToHere);//初始化title
    }

    @OnClick({R.id.hot_top_quit, R.id.hot_top_find})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hot_top_quit:
                finish();
                break;
            case R.id.hot_top_find:
                Bundle bundle = new Bundle();
                FoundGoodsActivity.jumpToFoundActivity(context,bundle);
                break;
        }
    }

    @Override
    public void showProgressDialog(){
        progressDialog = new MaterialDialog.Builder(context)
                .content("加载中...")
                .progress(true,0)
                .build();
        progressDialog.show();
    }

    @Override
    public void dismissProgressDialog(){
        progressDialog.dismiss();
    }

    @Override
    public void setDetailsAdapterNew(FoundGoodDetailAdapterNew adapter) {
        dismissProgressDialog();
        hotMarketDetailRecycler.setAdapter(adapter);
    }
}
